#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include "cont.h"

char* is_on(int num)
{
	return num ? "on" : "off";
}

char* get_align(int num)
{
	char* ret = NULL;
	switch (num)
	{
	case 0:
		ret = "left";
		break;
	case 1:
		ret = "center";
		break;
	case 2:
		ret = "right";
		break;
	default:
		printf("Wrong Alignment!\n");
	}
	return ret;
}

void show_cont(const struct Cont* pc)
{
	printf("\n");
	printf("%4s   %-6s %-9s %4s %4s %4s\n", "ID", "SIZE", "ALIGNMENT", "B", "I", "U");
	printf("%4d    %-7d %-8s %4s %4s %4s\n", pc->id, pc->size, get_align(pc->alignment), is_on(pc->b), is_on(pc->i), is_on(pc->u));
}

void menu()
{
	printf("\n");
	printf("%-20s %-20s %-20s\n", "f)change font", "s)change size", "a)change alignment");
	printf("%-20s %-20s %-20s\n", "b)toggle bold", "s)toggle italic", "a)toggle underline");
	printf("%-20s\n", "q)quit");
	printf("\n");
}

void eat_line()
{
	while (getchar() != '\n')
		continue;
}

void change_font(struct Cont* pc)
{
	int num = 0;
	do
	{
		printf("Change the font(0-255):");
		scanf("%d", &num);
		eat_line();
	} while (num > 255 || num < 0);
	
	pc->id = num;
}

void change_size(struct Cont* pc)
{
	int num = 0;
	do
	{
		printf("Change the font(0-127):");
		scanf("%d", &num);
		eat_line();
	} while (num > 127 || num < 0);

	pc->size = num;
}

void change_align(struct Cont* pc)
{
	char ch = '0';
	again:
	printf("l)left  c)center  r)right\n");
	printf("Please change the alignment:");
	ch = getchar();
	eat_line();
	switch (ch)
	{
	case 'l':
		pc->alignment = 0; break;
	case 'c':
		pc->alignment = 1; break;
	case 'r':
		pc->alignment = 2; break;
	default:
		printf("Wrong choice,input again\n");
		goto again;
	}
}

void change_bold(struct Cont* pc)
{
	pc->b = (pc->b) ? 0 : 1;
}

void change_italic(struct Cont* pc)
{
	pc->i = (pc->i) ? 0 : 1;
}

void change_underline(struct Cont* pc)
{
	pc->u = (pc->u) ? 0 : 1;
}